/*
 * @Author: lmk
 * @Date: 2021-11-17 19:44:50
 * @LastEditTime: 2021-12-01 14:35:40
 * @LastEditors: lmk
 * @Description: 
 */
import {add,update,deleteIds} from '@/api/crud.js'
export default {
	data(){
		return {
			addFlag:false
		}
	},
	methods:{
		removeIds(form){
			// deleteIds('',this.apiType,this.apiModule,'city/'+id).then(res=>{
			// 	this.$u.toast('删除成功')
			// 	this.getList&&this.getList();
			// 	this.close()
			// })
			if(!this.isCityListGroup){
				const removeIndex = this.citySelectList.findIndex(val=>val.cityId==form.cityId);
				if(removeIndex>-1){
					this.citySelectList.splice(removeIndex,1)
					this.$u.toast('删除成功')
					this.close()
				}
			}else{
				const {findLetterListIndex,getObjIndex} = this.getCityIndex(form); // 获取首字母所在的list
				if(getObjIndex>-1){
					this.citySelectList[findLetterListIndex].list.splice(getObjIndex,1)
					if(this.citySelectList[findLetterListIndex].list.length==0){
						this.citySelectList.splice(findLetterListIndex,1)
					}
					this.$u.toast('删除成功')
					this.close()
				}
			}
		},
		//删除回调
		removeItem(form){
			this.removeIds(form)
		},
		//选中回调
		select(item){
			this.$set(item,'address',this.cityPathFormat(item.cityPath))
			this.activeForm = this.$u.deepClone(item);
			this.addFlag = true;
			this.isCityEdit = true;
		},
		saveCity(form){
			const {totalNumber,grantNumber,address} = form;
			if(!address){
				this.$u.toast('请选择售卖城市')
				return false;
			}
			if(!totalNumber){
				this.$u.toast('请填写总计本数')
				return false;
			}
			if(!this.$u.test.digits(totalNumber)){
				this.$u.toast('总计本数只能为数字')
				return false;
			}
			if(grantNumber===''){
				this.$u.toast('请填写已授权本数')
				return false;
			}
			if(!this.$u.test.digits(grantNumber)){
				this.$u.toast('已授权本数只能为数字')
				return false;
			}
			if(Number(grantNumber)>Number(totalNumber)){
				this.$u.toast('已授权本数不能大于总计本数量')
				return false;
			}
			if(this.option.issueModeName==='独家'){
				//独家总计只能为1 授权数量为0或者1
				if(totalNumber!=1){
					this.$u.toast('总计本数只能为1')
					return false;
				}
				if(grantNumber>1&&grantNumber<0){
					this.$u.toast('已授权本数只能为0或者1')
					return false;
				}
			}
			form.id ? this.updateCity(form) : this.createCity(form)
			// const fn = form.id ? update : add;
			// form.productId = this.option.id;
			// fn(form,this.apiType,this.apiModule,'updateCity').then(res=>{
			// 	this.$u.toast('操作成功');
			// 	this.getList&&this.getList();
			// 	this.form = {
			// 		totalNumber:'',grantNumber:'',address:''
			// 	}
			// 	this.close()
			// })
		},
		showPop(){
			// this.activeForm = null;
			this.addFlag = true;
      this.activeForm = {}
		},
		close(){
			this.addFlag = false;
			this.isCityEdit = false;
			// this.activeForm = null;
      this.activeForm = {}
		},
		//获取当前活跃城市的下标
		getCityIndex(form,filterList=this.citySelectList){
			console.log(filterList)
			const getFormLetter = form.cityPinyin.substring(0,1); // 获取首字母
			const findLetterListIndex = filterList.findIndex(val=>val.py===getFormLetter); // 获取首字母所在的list
			const getObjIndex = findLetterListIndex>-1 ? filterList[findLetterListIndex].list.findIndex(val=>val.cityId==form.cityId) : -1
			return {
				getObjIndex,findLetterListIndex,getFormLetter
			}
		},
		//新增
		createCity(form){
			console.log(form)
			form.issueMode=this.option.issueMode;
			
			//新增需要判断本地是否有相同城市 如果有则不允许新增成功 
			const findIndex = this.citySelectList.findIndex(val=>val.cityId==form.cityId);
			if(findIndex>-1){
				this.$u.toast('请勿重新添加授权城市')
				return false;
			}
			// form.id= this.$u.guid(); //添加随机id表明是新增还是编辑
			const {findLetterListIndex,getFormLetter,getObjIndex} = this.getCityIndex(form); // 获取首字母所在的list
			console.log(getObjIndex)
			if(getObjIndex>-1){
				//新增的时候查询到列表里面有这个数据项目 则直接更新
				this.updateCity(form);
				return false;
			}
			if(findLetterListIndex>-1){
				this.citySelectList[findLetterListIndex].list.push(form); // 直接添加数据
				this.close()
				return false;
			}
			if(getFormLetter){
				this.citySelectList.push({
					py:getFormLetter,
					list:[form]
				})
				this.close()
			}
			
		},
		//修改
		updateCity(form){
			if(!this.isCityListGroup){
				const findIndex = this.citySelectList.findIndex(val=>val.cityId==form.cityId);
				if(findIndex>-1){
					this.citySelectList[findIndex] = form;
					this.citySelectList = [...this.citySelectList]
					this.$u.toast('修改成功')
					this.close()
				}
			}else{
				const {findLetterListIndex,getObjIndex} = this.getCityIndex(form); // 获取首字母所在的list
				if(getObjIndex>-1){
					this.citySelectList[findLetterListIndex].list[getObjIndex] = form; //直接赋值修改
					this.$u.toast('修改成功')
					this.close()
				}
			}
		}
	}
}